﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;

namespace ncp.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Ads runat=server></{0}:Ads>")]
    public class Ads : WebControl
    {
        /// <summary>
        /// 广告类型
        /// </summary>
        [Bindable(true)] //设置允许绑定
        [DefaultValue("")] //默认值为空
        [Localizable(true)] //允许本地化
        public string Type { get; set; }

        /// <summary>
        /// 广告Id
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string Adid{ get; set; }

        /// <summary>
        /// 广告类别
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string AdClass { get; set; }

        /// <summary>
        /// CSS样式
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string CssStyle{ get; set; }

        /// <summary>
        /// class
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string AdCssClass { get; set; }

        //设置内容CSS
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string ContentCssStyle{ get; set; }

        /// <summary>
        /// 显示条数
        /// </summary>
        [Bindable(true)]
        [DefaultValue(1)]
        [Localizable(true)]
        public int ShowNumber{ get; set; }

        /// <summary>
        /// 广告为空时显示的内容
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string DefaultText { get; set; }

        /// <summary>
        /// 查询第几条到第几条记录，用“,”号隔开
        /// </summary>
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string IntervalAds { get; set; }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" endtime>=getdate() and id is not null ");

            if (!string.IsNullOrEmpty(Type))
            {
                builder.AppendFormat(" and type in({0})", Type);
            }
            if (!string.IsNullOrEmpty(AdClass))
            {
                builder.AppendFormat(" and adid in({0})", AdClass);
            }
            if (!string.IsNullOrEmpty(Adid))
            {
                builder.AppendFormat(" and id in({0})", Adid);
            }
            DataTable adTable;
            if (!string.IsNullOrEmpty(IntervalAds))
            {
                var limit = IntervalAds.Split(',');
                adTable = ADInfoBll.GetAdInfoBetween("*", builder.ToString(), limit[0], limit[1], CommonConst.DB_Name);
            }
            else
            {
                string fields = " top " + ShowNumber + " *";

                adTable = ADInfoBll.GetADInfoTable(fields, builder.ToString(), "sort asc", CommonConst.DB_Name);
            }


            StringBuilder adHtml = new StringBuilder();
            adHtml.AppendFormat("<div style=\"{0}\" class=\"{1}\">", CssStyle, AdCssClass);
            foreach (DataRow row in adTable.Rows)
            {
                if (!string.IsNullOrEmpty(ContentCssStyle))
                {
                    adHtml.AppendFormat("<div class=\"{0}\">", ContentCssStyle);
                }
                switch (row["type"].ToString())
                {
                    case "1":
                        adHtml.Append(row["html"]);
                        break;
                    case "2":
                        if (row["url"]!=DBNull.Value&& row["url"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat("<a href=\"{0}\" target=\"_blank\" style=\"", row["url"]);
                            if (row["color"] != DBNull.Value && row["color"].ToString().Trim() != "")
                            {
                                adHtml.AppendFormat(" color:{0}; ", row["color"]);
                            }
                            if (row["fontsize"] != DBNull.Value && row["fontsize"].ToString().Trim() != "")
                            {
                                adHtml.AppendFormat(" font-size:{0}; ", row["fontsize"]);
                            }
                            adHtml.Append("\">");
                        }
                        adHtml.Append(row["content"]);

                        if (row["url"].ToString().Trim() != "")
                        {
                            adHtml.Append("</a>");
                        }
                        break;
                    case "3":
                        if (row["url"] != DBNull.Value && row["url"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat("<a href=\"{0}\" target=\"_blank\" title=\"{1}\">", row["url"],row["content"]);
                        }
                        adHtml.AppendFormat("<img src=\"{0}\" style=\"",row["picture"]);
                        if (row["width"] != DBNull.Value && row["width"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat(" width:{0}; ", row["width"]);
                        }
                        if (row["height"] != DBNull.Value && row["height"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat(" height:{0}; ", row["height"]);
                        }
                        adHtml.Append("\"");

                        if (Height.ToString() != "" && row["height"] != DBNull.Value && row["height"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat(" height=\"{0}\" ", Height);
                        }
                        if (Width.ToString() != "" && row["width"] != DBNull.Value && row["width"].ToString().Trim() != "")
                        {
                            adHtml.AppendFormat(" width=\"{0}\" ", Width);
                        }
                        builder.Append("/>");
                        if (row["url"] != DBNull.Value && row["url"].ToString().Trim() != "")
                        {
                            adHtml.Append("</a>");
                        }
                        break;
                    case "4":
                        adHtml.Append(
                            "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"");
                        adHtml.AppendFormat(" width=\"{0}\" height=\"{1}\">", row["width"], row["height"]);
                        adHtml.AppendFormat(" <param name=\"movie\" value=\"{0}\">", row["url"]);
                        adHtml.Append(" <param name=\"quality\" value=\"high\">");
                        adHtml.Append(" <param name='wmode' value=\"transparent\">");
                        adHtml.AppendFormat(
                            " <embed src=\"{0}\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"",
                            row["url"]);
                        adHtml.AppendFormat(
                            " type=\"application/x-shockwave-flash\" width=\"{0}\" height=\"{1}\"></embed></object>",
                            row["width"], row["height"]);
                        break;
                }
                if (!string.IsNullOrEmpty(ContentCssStyle))
                {
                    adHtml.Append("</div>");
                }
            }
            if (adTable.Rows.Count < ShowNumber)
            {
                for (int i = 0; i < ShowNumber - adTable.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(ContentCssStyle))
                    {
                        adHtml.AppendFormat("<div class=\"{0}\">", ContentCssStyle);
                    }
                    adHtml.Append(DefaultText);
                    if (!string.IsNullOrEmpty(ContentCssStyle))
                    {
                        adHtml.Append("</div>");
                    }
                }
            }
            adHtml.Append("</div>");
            output.Write(adHtml.ToString());
        }
    }
}
